package com.csproj.siiat.base.common.util.TLMessage;

import com.csproj.basic.domain.ElectronicLockMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import static com.csproj.siiat.base.common.util.TLMessage.TLMessage.*;


/**
 * Created by tyshi on 2018/9/26.
 * 
 * this is the parent class for TL messages sent from guansuo, so called TL
 * 
 */

public class C4MessageBody  extends TLMessageBody {

    private final Logger logger = LoggerFactory.getLogger(C4MessageBody.class);

    public static final int len = 46;

    private GPS gps = null;

    private class GPS {
        private String sudu = "";
        private String fangxiang = "";
        private String gaocheng = "";
        private String gaodu = "";
        private String number = "";
        private String[] idlist = new String[10];
        private String ids = "";
        private String bdnumber = "";
        private String bdid = "";
        private String version = "";
        private String gsm = "";
        private String dong = "";
        private String nian = "";
        private String yue = "";
        private String ri = "";
        private String shi = "";
        private String fen = "";
        private String miao = "";
        private String time = "";

        GPS (byte[] b) {
            int index = 0;
            byte[] tmp = new byte[2];
            System.arraycopy( b, index,tmp ,0, tmp.length);
            index +=tmp.length;
            sudu = bytes2Speed(tmp);
            tmp = new byte[3];
            System.arraycopy( b, index,tmp ,0, tmp.length);
            index += tmp.length;
            fangxiang = bytes2Fangxiang(tmp);
            tmp = new byte[3];
            System.arraycopy( b, index,tmp ,0, tmp.length);
            index += tmp.length;
            gaocheng = bytes2Gao(tmp);
            tmp = new byte[3];
            System.arraycopy( b, index,tmp ,0, tmp.length);
            index += tmp.length;
            gaodu = bytes2Gao(tmp);
            tmp = new byte[11];
            System.arraycopy( b, index,tmp ,0, tmp.length);
            index += tmp.length;
            number = byte2Int(tmp[0]);
            idlist[0] = byte2Int(tmp[1]);
            idlist[1] = byte2Int(tmp[2]);
            idlist[2] = byte2Int(tmp[3]);
            idlist[3] = byte2Int(tmp[4]);
            idlist[4] = byte2Int(tmp[5]);
            idlist[5] = byte2Int(tmp[6]);
            idlist[6] = byte2Int(tmp[7]);
            idlist[7] = byte2Int(tmp[8]);
            idlist[8] = byte2Int(tmp[9]);
            idlist[9] = byte2Int(tmp[10]);
            for (int i =0; i < Integer.valueOf(number);i++) {
                if (ids.equals("")) {
                    ids = idlist[i];
                } else {
                    ids = ids + "-" + idlist[i];
                }
            }
            tmp = new byte[11];
            System.arraycopy( b, index,tmp ,0, tmp.length);
            index += tmp.length;
            bdnumber = byte2Int(tmp[0]);
            bdid = byte2Int(tmp[1]);
            version = byte2Int(tmp[2]);
            gsm = byte2Int(tmp[3]);
            dong = byte2Int(tmp[4]);
            nian = byte2Int(tmp[5]);
            yue = byte2Int(tmp[6]);
            ri = byte2Int(tmp[7]);

            shi = byte2Int(tmp[8]);
            fen = byte2Int(tmp[9]);
            miao = byte2Int(tmp[10]);
            //TODO change the time format
            time = nian + "-" + yue + "-" + ri + " " + shi + ":" + fen + ":" + miao;

        }
        private String bytes2Speed (byte[] b) {
            String x =  byte2Int(b[0]);
            String y = byte2Int(b[1]);
            float a = Float.valueOf(x+"."+y) * JIE;
            return String.valueOf(a);
        }

        private String bytes2Fangxiang (byte[] b) {
            byte[] a = new byte[2];
            System.arraycopy( b, 0,a ,0, a.length);
            //TODO: need to verify the logic here
//            String x =  byte2Int(b[0]);
//            String y = byte2Int(b[1]);
            String n = bytes2Int(a);
            String z = byte2Int(b[2]);

//            return x+y+"."+z;
            return n+"."+z;

        }

        private String bytes2Gao (byte[] b) {
            byte[] a = new byte[2];
            System.arraycopy( b, 0,a ,0, a.length);
            String n = bytes2Int(a);
            String x =  byte2String(b[0]);
//            String y = byte2Int(b[1]);
            String z = byte2Int(b[2]);
            if (x.startsWith("1")) {
                return "-"+n+"."+z;
            } else {
                return n+"."+z;

            }

        }

        @Override
        public String toString () {

            return "Sudu: " + sudu + "\n"
                    +"Fangxiang: " + fangxiang + "\n"
                    +"Gaocheng: " + gaocheng + "\n"
                    +"Gaodu: " + gaodu + "\n"
                    +"SuoJuStatus: " + dong + "\n"
                    +"Number: " + number + "\n"
                    +"GPS List: " + ids + "\n"
                    +"BDNumber: " + bdnumber + "\n"
                    +"GSM: " + gsm + "\n"
                    +"Time: " + time +"\n";
        }
        public ElectronicLockMessage toDomain(ElectronicLockMessage elm) {
        	
        	ZoneId zone = ZoneId.of("Europe/London");
        	ZonedDateTime zonet = ZonedDateTime.of(
        			Integer.valueOf("20"+nian), 
        			Integer.valueOf(yue), 
        			Integer.valueOf(ri), 
        			Integer.valueOf(shi), 
        			Integer.valueOf(fen), 
        			Integer.valueOf(miao),0,zone);
            elm.setSpeed(Float.valueOf(sudu));
            elm.setDirection(Float.valueOf(fangxiang));
            elm.setAltitude(Float.valueOf(gaocheng));
            elm.setHight(Float.valueOf(gaodu));
            elm.setGpsStarNumber(Integer.valueOf(number));
            elm.setGpsStarIds(ids);
            elm.setBdStarNumber(Integer.valueOf(bdnumber));
            elm.setBdStarIds(bdid);
            elm.setGsmSignal(Integer.valueOf(gsm));
            elm.setIsMoving(Integer.valueOf(dong));
            elm.setTime(zonet);
            return elm;
        }


    }
    public C4MessageBody(byte[] b) {
        int index = 0;
        byte[] tmp = new byte[2];
        System.arraycopy( b, index,tmp ,0, tmp.length);
        index +=tmp.length;
        status = bytes2Status(tmp);
        tmp = new byte[4];
        System.arraycopy( b, index,tmp ,0, tmp.length);
        index += tmp.length;
        WeiDu = bytes2LocationW(tmp);
        tmp = new byte[5];
        System.arraycopy( b, index,tmp ,0, tmp.length);
        index += tmp.length;
        JingDu = bytes2LocationJ(tmp);
        tmp = new byte[2];
        System.arraycopy( b, index,tmp ,0, tmp.length);
        index += tmp.length;
        vol = bytes2Vol(tmp);
        tmp = new byte[33];
        System.arraycopy( b, index,tmp ,0, tmp.length);
        index += tmp.length;
        gps = new GPS(tmp);

    }

    @Override
    public String toString () {
        return "Status: " + status + "\n"
                +"ShiFengStatus: " + ShiFengStatus + "\n"
                +"KaiSuoStatus: " + KaiSuoStatus + "\n"
                +"GuanSuoStatus: " + GuanSuoStatus + "\n"
                +"WeiDu: " + WeiDu + "\n"
                +"JingDu: " + JingDu + "\n"
                +"Volt: " + vol + "\n"
                +gps.toString();
    }
    @Override
    public ElectronicLockMessage toDomain(ElectronicLockMessage elm) {
        if (Integer.valueOf(SuoGanStatus) == 0) {
            elm.setLockStickStatus(false);
        } else {
            elm.setLockStickStatus(true);
        }
        if (Integer.valueOf(WaiKeStatus) == 0) {
            elm.setOuterStatus(false);
        } else {
            elm.setOuterStatus(true);
        }
        if (Integer.valueOf(GPSStatus) == 0) {
            elm.setGpsStatus(false);
        } else {
            elm.setGpsStatus(true);
        }
        if (Integer.valueOf(ShiFengStatus) == 0) {
            elm.setIsLocked(false);
        } else {
            elm.setIsLocked(true);
        }
        if (Integer.valueOf(KaiSuoStatus) == 0) {
            elm.setIsOpen(false);
        } else {
            elm.setIsOpen(true);
        }
        elm.setLatitude(Float.valueOf(WeiDu));
        elm.setLongitude(Float.valueOf(JingDu));
        elm.setVolt(Integer.valueOf(vol));
        gps.toDomain(elm);
        return elm;
    }

}
